﻿using PedestrianSensingRadar.Utility;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PedestrianSensingRadar
{
    public partial class MessageCenter : Form
    {
        private static MessageCenter uniqueInstance;//私有变量来记录MessageCenter的唯一实例

        // 定义一个标识确保线程同步
        private static readonly object locker = new object();


        public bool Getcb_sigalchecked
        {
            get { return cb_sigal.Checked; }
        }

        public bool Cb_objchecked
        {
            get { return cb_obj.Checked; }
        }

        private MessageCenter(SerDataparser ser)
        {
            InitializeComponent();
           
            ser.ShowdataR += new SerDataparser.showdataReventHandle(Ser_showdataR);            
        }

        public static MessageCenter GetInstance(SerDataparser ser)
        {
            if (uniqueInstance == null)
            {
                lock (locker)
                {
                    if (uniqueInstance == null)
                    {
                        uniqueInstance = new MessageCenter(ser);
                    }
                }
            }
            return uniqueInstance;
        }
   
        private void Ser_showdataR(PacketBuff_t buff, ushort pos_header, int frame_len)
        {          
            Action act = delegate ()
            {
                for (int i = pos_header; i < pos_header + frame_len; i++)
                {
                    string str = Convert.ToString(buff.pack_buff[i], 16).ToUpper();          //将数据转换为字符串格式
                    tb_dataR.AppendText((str.Length == 1 ? "0" + str + " " : str + " "));   //添加到串口接收文本框中
                }
                tb_dataR.AppendText("\r\n");
            };
            Invoke(act);           
        }

        public void Frmmain_showsendEvent(byte[] data)
        {
            for (int i = 0; i < data.Length; i++)
            {
                string str = Convert.ToString(data[i], 16).ToUpper();          //将数据转换为字符串格式
                tb_dataS.AppendText((str.Length == 1 ? "0" + str + " " : str + " "));   //添加到串口接收文本框中
            }
            tb_dataS.AppendText("\r\n");
        }

        public void dataParseTest(byte[] data)
        {
            float Hori_angle, Pitch_angle, Height;
            PointF p1 = new PointF();
            PointF p2 = new PointF();
            PointF p3 = new PointF();
            PointF p4 = new PointF();
            PointF p5 = new PointF();
            PointF p6 = new PointF();
            PointF p7 = new PointF();
            PointF p8 = new PointF();

            string str = "";

            int head = 5;
            Hori_angle = ((data[head + 2] << 8) + data[head + 1]) / 10;
            Pitch_angle = ((data[head + 4] << 8) + data[head + 3]) / 10;
            Height = data[head + 5] / 10;

            p1.X = (float)((short)((data[head + 7] << 8) + data[head + 6])/10.0) ;
            p1.Y = (float)((short)((data[head + 9] << 8) + data[head + 8]) /10.0);

            p2.X = (float)((short)((data[head + 11] << 8) + data[head + 10]) / 10.0);
            p2.Y = (float)((short)((data[head + 13] << 8) + data[head + 12]) / 10.0);

            p3.X = (float)((short)((data[head + 15] << 8) + data[head + 14]) / 10.0);
            p3.Y = (float)((short)((data[head + 17] << 8) + data[head + 16]) / 10.0);

            p4.X = (float)((short)((data[head + 19] << 8) + data[head + 18]) / 10.0);
            p4.Y = (float)((short)((data[head + 21] << 8) + data[head + 20]) / 10.0);

            p5.X = (float)((short)((data[head + 24] << 8) + data[head + 23]) / 10.0);
            p5.Y = (float)((short)((data[head + 26] << 8) + data[head + 25]) / 10.0);

            p6.X = (float)((short)((data[head + 28] << 8) + data[head + 27]) / 10.0);
            p6.Y = (float)((short)((data[head + 30] << 8) + data[head + 29]) / 10.0);

            p7.X = (float)((short)((data[head + 32] << 8) + data[head + 31]) / 10.0);
            p7.Y = (float)((short)((data[head + 34] << 8) + data[head + 33]) / 10.0);

            p8.X = (float)((short)((data[head + 36] << 8) + data[head + 35]) / 10.0);
            p8.Y = (float)((short)((data[head + 38] << 8) + data[head + 37]) / 10.0);

            str = "水平角度：" + Hori_angle.ToString() + " " +
                "俯仰角度：" + Pitch_angle.ToString()+" " +
                 "高度：" + Height.ToString() + " " +
                 "p1:" + "(" + p1.X.ToString() + "," + p1.Y.ToString() + ")" + " " +
                 "p2:" + "(" + p2.X.ToString() + "," + p2.Y.ToString() + ")" + " " +
                 "p3:" + "(" + p3.X.ToString() + "," + p3.Y.ToString() + ")" + " " +
                 "p4:" + "(" + p4.X.ToString() + "," + p4.Y.ToString() + ")" + " " +
                 "p5:" + "(" + p5.X.ToString() + "," + p5.Y.ToString() + ")" + " " +
                 "p6:" + "(" + p6.X.ToString() + "," + p6.Y.ToString() + ")" + " " +
                 "p7:" + "(" + p7.X.ToString() + "," + p7.Y.ToString() + ")" + " " +
                 "p8:" + "(" + p8.X.ToString() + "," + p8.Y.ToString() + ")" + "\r\n";

            tb_dataS.AppendText(str);
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            tb_dataR.Text = "";
        }

        private void MessageCenter_FormClosing(object sender, FormClosingEventArgs e)
        {
            Hide();
            e.Cancel = true;
        }

        private void cb_obj_CheckedChanged(object sender, EventArgs e)
        {
            Common.isobj = cb_obj.Checked;
        }

        private void cb_sigal_CheckedChanged(object sender, EventArgs e)
        {
            Common.is_sigal = cb_sigal.Checked;
        }

 
    }
}
